<?

/**
 * MySql Database interraction
 * 
 * adServer.class.php
 *
 * @package adServer
 */
class database {

    var $db;
     /* Handle on the DB instance */

    /**
     * Constructor
     * 
     * @param string $dbHost
     * @param string $dbUsername
     * @param string $dbPassword
     * @param string $dbName
     * @param bool $select [optional]
     * @return 
     */
    function database($dbHost, $dbUsername, $dbPassword, $dbName, $select = true) {
        $this->db = @mysql_connect($dbHost, $dbUsername, $dbPassword) or die("Problem connecting to the Database:<br>" .
            mysql_error());

        /* Select the DB, if parameter set to true */
        if ($select == true) {
            mysql_select_db($dbName) or die(mysql_error());
        }
    }

    /**
     * Select specified DB 
     * 
     * @param object $dbName
     * @return 
     */
    function selectDb($dbName) {
        return mysql_select_db($dbName);
    }


    /**
     * Perform a DB query
     * 
     * @param object $qry
     * @return 
     */
    function qry($qry) {
        //opens db connection
        $results = mysql_query($qry) or die("Problem connecting to the Database:<br>" .
            mysql_error());
        return $results;
    }

    /**
     * Retrieve results from a DB Query (Associative array format)
     * 
     * @param object $qry
     * @return 
     */
    function get_results($qry) {
        //returns results as an array. $results[$i][$index]
        $result = $this->qry($qry);
        $count = mysql_num_rows($result);

        for ($i = 1; $i <= $count; $i++) {
            $results[$i] = mysql_fetch_assoc($result);
        }
        if ($count >= 1) {
            return $results;
        } else {
            return false;
        }
    }

    /**
     * Retrieve results from a DB Query (in an Array)
     * 
     * @param String $qry
     * @return 
     */
    function get_results_array($qry) {
        //returns results as an array. $results[$i][$index]
        $result = $this->qry($qry);
        $count = mysql_num_rows($result);

        for ($i = 1; $i <= $count; $i++) {
            $results[$i] = mysql_fetch_row($result);
        }
        if ($count >= 1) {
            return $results;
        } else {
            return false;
        }
    }

    /**
     * Test Boolean result of a query
     * 
     * @param string $qry
     * @return 
     */
    function get_result_bool($qry) {
        $result = $this->qry($qry);
        $rowResult = mysql_fetch_row($result);
        return $rowResult[0] == 1;
    }

    /**
     * Test if a table exists
     *  @param String $tableName
     * @return true if table exists, false if doesn't exist
     */    
    function table_exists($tableName){
        $tableName=mysql_real_escape_string($tableName);
        $qry="SHOW tables LIKE '$tableName'";
        return $this->get_results_array($qry)!=false;         
    }

    function destroy() {
        mysql_close($this->db);
        unset($this->db);
    }
}

?>